// 自定义错误
// 当错误发生的时候，我们捕获到发生的错误，然后抛出我们自定义的错误

/**
 * 业务处理错误基类
 */

const { FormatJson } = require("./tool");

class ServiceError extends Error {
    constructor(message, code) {
        super(message);
        this.code = code;
    }

    // 方法
    toResponseJSON() {
        return this.code != 405 ? FormatJson(this.code, this.message, null) : FormatJson(this.code, "", JSON.parse(this.message))
    }
}

// 文件上传错误
exports.UploadError = class extends ServiceError {
    constructor(message) {
        super(message, 413);
    }
}

// 禁止访问错误
exports.ForbiddenError = class extends ServiceError {
    constructor(message) {
        super(message, 401)
    }
}

// 验证码错误
exports.ValidationError = class extends ServiceError {
    constructor(message) {
        super(message, 403)
    }
}

// 文件未找到错误
exports.NotFoundError = class extends ServiceError {
    constructor() {
        super("not found", 406)
    }
}

// 未通过评审
exports.DataFaildError = class extends ServiceError {
    constructor(message) {
        super(message, 405)
    }
}

// 未知错误
exports.UnknownError = class extends ServiceError {
    constructor() {
        super("server internal error", 500);
    }
}

exports.ServiceError = ServiceError;